﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Hack.Forms
{  
    ///====================================================
    ///| Downloaded From                                  |
    ///| Visual C# Kicks - http://www.vcskicks.com/       |
    ///| License - http://www.vcskicks.com/license.html   |
    ///====================================================
    class WindowStateManager
    {
        [DllImport("user32.dll")]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        [DllImport("user32.dll")]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr hWnd);

        private struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public int showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        private enum MainWindowState
        {
            Normal = 1,
            Minimized,
            Maximized
        }

        private static List<IntPtr> NormalWindows;
        private static List<IntPtr> MaximizedWindows;

        public static void MinimizeAll(IntPtr exclude)
        {
            NormalWindows = new List<IntPtr>();
            MaximizedWindows = new List<IntPtr>();

            foreach (Process prc in Process.GetProcesses())
            {
                if (prc.MainWindowTitle != string.Empty &&
                    prc.MainWindowHandle != exclude) //window to exclude
                {
                    //Get the window's current state
                    WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
                    placement.length = Marshal.SizeOf(placement);
                    GetWindowPlacement(prc.MainWindowHandle, ref placement);
                    MainWindowState state = (MainWindowState)placement.showCmd;

                    if (state != MainWindowState.Minimized)
                    {
                        //Store what the previous state of the window was
                        if (state == MainWindowState.Normal)
                            NormalWindows.Add(prc.MainWindowHandle);
                        else
                            MaximizedWindows.Add(prc.MainWindowHandle);

                        //Minimize it
                        ShowWindow(prc.MainWindowHandle, (int)MainWindowState.Minimized);
                    }
                }
            }
        }

        public static void MaximizeAll(IntPtr topWindow)
        {
            if (MaximizedWindows != null)
            {
                //Restore previously maximized windows
                foreach (IntPtr handle in MaximizedWindows)
                {
                    ShowWindow(handle, (int)MainWindowState.Maximized);
                }
            }

            if (NormalWindows != null)
            {
                //Restore previously normal windows
                foreach (IntPtr handle in NormalWindows)
                {
                    ShowWindow(handle, (int)MainWindowState.Normal);
                }
            }

            SetForegroundWindow(topWindow);
        }

    }

}
